/*
 * $Id: UserInterfaceDAO.java,v 1.11 2012/04/29 22:00:34 navdeep_mahajan Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.ui.dao;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.database.dao.IFileTransferDetailsDAO;
import com.ge.healthcare.autosc.common.database.dao.IStepLogsDAO;
import com.ge.healthcare.autosc.common.database.pojo.AbstractRequest;
import com.ge.healthcare.autosc.common.database.pojo.AbstractRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.ActFileTransferDetails;
import com.ge.healthcare.autosc.common.database.pojo.ActRequest;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestList;
import com.ge.healthcare.autosc.common.database.pojo.ActRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.ActTask;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestDefn;
import com.ge.healthcare.autosc.common.database.pojo.CfgRequestType;
import com.ge.healthcare.autosc.common.database.pojo.CfgStepType;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskCategoryInfo;
import com.ge.healthcare.autosc.common.database.pojo.CfgTaskType;
import com.ge.healthcare.autosc.common.database.pojo.HstFileTransferDetails;
import com.ge.healthcare.autosc.common.database.pojo.HstRequest;
import com.ge.healthcare.autosc.common.database.pojo.HstRequestList;
import com.ge.healthcare.autosc.common.database.pojo.HstRequestListUser;
import com.ge.healthcare.autosc.common.database.pojo.HstStepLogs;
import com.ge.healthcare.autosc.common.database.pojo.HstStepTime;
import com.ge.healthcare.autosc.common.database.pojo.HstTask;
import com.ge.healthcare.autosc.ui.pojo.UiFileDetails;
import com.ge.healthcare.autosc.ui.pojo.UiRequest;
import com.ge.healthcare.autosc.ui.pojo.UiRequestList;
import com.ge.healthcare.autosc.ui.pojo.UiRqUser;
import com.ge.healthcare.autosc.ui.pojo.UiStepLog;
import com.ge.healthcare.autosc.ui.pojo.UiTask;

public class UserInterfaceDAO extends HibernateDaoSupport {
	private ICodeDetailsDAO codeDetailsDAO;
	private IStepLogsDAO stepLogsDAO;
	private IFileTransferDetailsDAO fileDetailsDAO;

	private StringBuffer getRequestSql(String table) {
		StringBuffer query = new StringBuffer("Select ");
		query.append("request, code ");
		query.append(" from ");
		query.append(table).append(" as request , ");
		query.append(CfgCodeDetails.TABLE_NAME).append(" as code ");
		query.append(" where ");
		query.append(" request.").append(ActRequest.COL_CODE_ID).append(" = ");
		query.append(" code.").append(CfgCodeDetails.COL_CODE_ID);
		query.append(" and ");
		return query;
	}

	private StringBuffer getRequestsSql(String table, int daysBack) {
		// Note that the ActRequest column variables are being used at this time
		// simply because passing them seems like a waste. If the table column
		// names change then it could be an issue
		StringBuffer query = getRequestSql(table);
		query.append(" request.").append(ActRequest.COL_SYSTEM_ID);
		query.append(" = ?").append(" and ");
		query.append(" request.").append(ActRequest.COL_START_TIME);
		query.append(" <= ( to_date( ?, 'YYYY/MM/DD') + 1)");
		query.append(" and ");
		query.append(" request.").append(ActRequest.COL_START_TIME);
		query.append(" >= ( to_date( ?, 'YYYY/MM/DD') - ");
		query.append(daysBack).append(")");
		query.append(" order by request.").append(ActRequest.COL_START_TIME);
		//query.append(" desc ");
		return query;
	}
	
	private StringBuffer getSwdRequestSql(String table, String userTable){
		StringBuffer query = new StringBuffer("Select ");
		query.append("request, code, crd, crt, user ");
		query.append(" from ");
		query.append(table).append(" as request,  ");
		query.append(CfgRequestDefn.TABLE_NAME).append(" as crd, ");
		query.append(CfgRequestType.TABLE_NAME).append(" as crt, ");
		query.append(CfgCodeDetails.TABLE_NAME).append(" as code, ");
		query.append(userTable).append(" as user ");
		query.append(" where ");
		query.append(" request.").append(ActRequest.COL_REQUEST_DEFN_ID).append(" = ");
		query.append(" crd.").append(CfgRequestDefn.COL_DEFN_ID);
		query.append(" and ");
		query.append(" crd.").append(CfgRequestDefn.COL_TYPE_ID).append(" = ");
		query.append(" crt.").append(CfgRequestType.COL_REQUEST_TYPE_ID);
		query.append(" and ");
		query.append(" request.").append(ActRequest.COL_CODE_ID).append(" = ");
		query.append(" code.").append(CfgCodeDetails.COL_CODE_ID);
		query.append(" and ");
		query.append(" request.").append(ActRequest.COL_REQUEST_LIST_ID).append(" = ");
		query.append(" user.").append(ActRequestListUser.COL_REQUEST_LIST_ID);
		query.append(" and ");
		query.append(" user.").append(ActRequestListUser.COL_ADDRESS_TYPE).append(" = ?");
		query.append(" and ");
		query.append(" crt.").append(CfgRequestType.COL_REQUEST_TYPE_NAME).append(" = ?");
		query.append(" order by request.").append(ActRequest.COL_SYSTEM_ID).append(",");
		query.append(" request.").append(ActRequest.COL_START_TIME);
		return query;		
	}
	
	public List<UiRequest> getSwdRequests(String requestTypeName) {
		System.out.println("Getting active requests");
		List<UiRequest> requests = getSwdRequestsByTable(requestTypeName, ActRequest.TABLE_NAME, ActRequestListUser.TABLE_NAME);
		if (ASCUtil.isNullOrEmptyList(requests)) {
			requests = new ArrayList<UiRequest>();
		}
		System.out.println("Getting historical requests");
		List <UiRequest> actRequest = getSwdRequestsByTable(requestTypeName, HstRequest.TABLE_NAME, HstRequestListUser.TABLE_NAME);
		if(!ASCUtil.isNullOrEmptyList(actRequest)){
			requests.addAll(actRequest);
		}
		return requests;
	}
	
	private List<UiRequest> getSwdRequestsByTable(String requestTypeName, String tableName, String userTable) {
		StringBuffer query = getSwdRequestSql(tableName, userTable);
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Object[] { ActRequestListUser.SENT_FROM_ADDRESS_TYPE, requestTypeName });
		List<UiRequest> requests = new ArrayList<UiRequest>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiRequest request = new UiRequest((ActRequest) list[column++]);
				request.load((CfgCodeDetails) list[column++]);
				request.load((CfgRequestDefn) list[column++]);
				request.load((CfgRequestType) list[column++]);
				request.load((ActRequestListUser) list[column++]);
				requests.add(request);
			}
		}
		return requests;		
	}
	
	private List<UiRequest> buildUiRequest(String query, String requestTypeName){
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Object[] { requestTypeName });
		List<UiRequest> requests = new ArrayList<UiRequest>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiRequest request = new UiRequest((HstRequest) list[column++]);
				request.load((CfgCodeDetails) list[column++]);
				loadRequestTypeNDefn(request);
				requests.add(request);
			}
		}
		return requests;		
	}
	
	
	
	public List<UiRequest> getRequests(String systemId, String startDate,
			int daysBack) {
		List<UiRequest> requests = getHstRequests(systemId, startDate, daysBack);
		if (ASCUtil.isNullOrEmptyList(requests)) {
			requests = new ArrayList<UiRequest>();
		}
		List <UiRequest> actRequest = getActRequests(systemId, startDate, daysBack);
		if(!ASCUtil.isNullOrEmptyList(actRequest)){
			requests.addAll(actRequest);
		}
		return requests;
	}

	public List<UiRequest> getHstRequests(String systemId, String startDate,
			int daysBack) {
		StringBuffer query = getRequestsSql(HstRequest.TABLE_NAME, daysBack);
		StringBuffer msg = new StringBuffer("query: ");
		msg.append(query.toString());
		msg.append(" system id: ").append(systemId);
		msg.append(" startDate: ").append(startDate);
		ASCLogger.debug(msg.toString());
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Object[] { systemId, startDate, startDate });
		List<UiRequest> requests = new ArrayList<UiRequest>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiRequest request = new UiRequest((HstRequest) list[column++]);
				request.load((CfgCodeDetails) list[column++]);
				loadRequestTypeNDefn(request);
				requests.add(request);
			}
		}
		return requests;
	}

	public List<UiRequest> getActRequests(String systemId, String startDate,
			int daysBack) {
		StringBuffer query = getRequestsSql(ActRequest.TABLE_NAME, daysBack);
		StringBuffer msg = new StringBuffer("query: ");
		msg.append(query.toString());
		msg.append(" system id: ").append(systemId);
		msg.append(" startDate: ").append(startDate);
		ASCLogger.debug(msg.toString());
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Object[] { systemId, startDate, startDate });
		List<UiRequest> requests = new ArrayList<UiRequest>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiRequest request = new UiRequest((ActRequest) list[column++]);
				request.load((CfgCodeDetails) list[column++]);
				loadRequestTypeNDefn(request);
				requests.add(request);
			}
		}
		return requests;
	}

	public UiRequest getRequest(String requestId) {
		UiRequest request = getHstRequest(requestId);
		if (ASCUtil.isNullOrEmptyObject(request.getRequestId())) {
			request = getActRequest(requestId);
		}
		return request;
	}

	private UiRequest getHstRequest(String requestId) {
		StringBuffer query = getRequestSql(HstRequest.TABLE_NAME);
		query.append(" request.").append(ActRequest.COL_REQUEST_ID);
		query.append(" = ? ");

		ASCLogger.debug("query: " + query.toString() + " value: " + requestId);
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Long(requestId));
		UiRequest request = new UiRequest();

		if (!ASCUtil.isNullOrEmptyList(values)) {
			int i = 0;
			Object[] list = (Object[]) values.get(0);
			request.load((HstRequest) list[i++]);
			request.load((CfgCodeDetails) list[i++]);
			if (!ASCUtil.isNullOrEmptyObject(request.getRequestDefnId())) {
				loadRequestTypeNDefn(request);
				loadHstTasks(request);
			}

		}
		return request;
	}

	private UiRequest getActRequest(String requestId) {
		StringBuffer query = getRequestSql(ActRequest.TABLE_NAME);
		query.append(" request.").append(ActRequest.COL_REQUEST_ID).append(
				" = ?");

		ASCLogger.debug("query: " + query.toString() + " value: " + requestId);
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Long(requestId));
		UiRequest request = new UiRequest();

		if (!ASCUtil.isNullOrEmptyList(values)) {
			int i = 0;
			Object[] list = (Object[]) values.get(0);
			request.load((ActRequest) list[i++]);
			request.load((CfgCodeDetails) list[i++]);
			if (!ASCUtil.isNullOrEmptyObject(request.getRequestDefnId())) {
				loadRequestTypeNDefn(request);
				loadActTasks(request);
			}

		}
		return request;
	}

	public List<UiStepLog> getStepLogs(String type, String id) {
		List<UiStepLog> logs = new ArrayList<UiStepLog>();
		StringBuffer query = new StringBuffer("Select ");
		query.append(" sTime, sType ");
		query.append(" from ");
		query.append(HstStepTime.TABLE_NAME).append(" as sTime , ");
		query.append(CfgStepType.TABLE_NAME).append(" as sType ");
		query.append(" where ");
		query.append(" sTime.").append(HstStepTime.COL_STEP_TYPE_ID);
		query.append(" = ");
		query.append(" sType.").append(CfgStepType.COL_ID);
		query.append(" and ");
		query.append(" sTime.").append(HstStepTime.COL_OBJECT_TYPE);
		query.append(" = ?").append(" and ");
		query.append(" sTime.").append(HstStepTime.COL_OBJECT_ID);
		query.append(" = ?");
		query.append(" order by sTime.").append(HstStepTime.COL_START_TIME);
		query.append(" desc ");

		ASCLogger.debug("query: " + query.toString() + " value: " + type
				+ " and " + id);
		List<?> values = getHibernateTemplate().find(query.toString(),
				new Object[] { type, new Long(id) });
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				HstStepTime stepTime = (HstStepTime) list[column++];
				UiStepLog stepLog = new UiStepLog(stepTime);
				stepLog.load((CfgStepType) list[column++]);
				if (!ASCUtil.isNullOrEmptyObject(stepLog.getCodeId())) {
					stepLog.load(codeDetailsDAO.getCodeDetailsByID(stepLog
							.getCodeId()));
				}
				List<HstStepLogs> stepLogs = stepLogsDAO
						.getStepLogsByStepTimeId(stepTime);
				if (!ASCUtil.isNullOrEmptyList(stepLogs)) {
					for (HstStepLogs slog : stepLogs) {
						stepLog.addLogText(slog.getStepLogText());
					}
				}
				logs.add(stepLog);
			}
		}
		return logs;
	}

	public List<UiFileDetails> getRequestFiles(String requestIdStr) {
		Long requestId = new Long(requestIdStr);
		List<UiFileDetails> files = null;
		files = getHstRequestFiles(requestId);
		if (ASCUtil.isNullOrEmptyList(files)) {
			List<ActFileTransferDetails> actFiles = null;
			actFiles = fileDetailsDAO.getFileTransferLstByRequestId(requestId);
			if (!ASCUtil.isNullOrEmptyList(actFiles)) {
				for (ActFileTransferDetails file : actFiles) {
					UiFileDetails uiFile = new UiFileDetails(file);
					files.add(uiFile);
				}
			}
		}
		return files;
	}

	private List<UiFileDetails> getHstRequestFiles(Long requestId) {
		StringBuffer query = new StringBuffer("Select hFile from ");
		query.append(HstFileTransferDetails.TABLE_NAME).append(" as hFile ");
		query.append(" where ");
		query.append(" hFile.").append(HstFileTransferDetails.COL_REQUEST_ID);
		query.append(" = ?");

		ASCLogger.debug("query: " + query.toString() + " value: " + requestId);
		List<?> values = getHibernateTemplate().find(query.toString(),
				requestId);
		List<UiFileDetails> files = new ArrayList<UiFileDetails>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				UiFileDetails uiFile = new UiFileDetails(
						(HstFileTransferDetails) values.get(i));
				files.add(uiFile);
			}
		}
		return files;
	}

	public void loadActTasks(UiRequest request) {
		request.initTasks();
		StringBuffer query = getQueryTasks(ActTask.TABLE_NAME);

		ASCLogger.debug("query: " + query.toString() + " value: "
				+ request.getRequestDefnId());
		List<?> values = getHibernateTemplate().find(query.toString(),
				request.getRequestId());
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiTask task = new UiTask((ActTask) list[column++]);
				task.load((CfgTaskType) list[column++]);
				task.load((CfgTaskCategoryInfo) list[column++]);
				task.load((CfgStepType) list[column++]);
				if (!ASCUtil.isNullOrEmptyObject(task.getCodeId())) {
					task.load(codeDetailsDAO.getCodeDetailsByID(task
							.getCodeId()));
				}
				request.add(task);
			}
		}
	}

	public void loadHstTasks(UiRequest request) {
		request.initTasks();
		StringBuffer query = getQueryTasks(HstTask.TABLE_NAME);
		ASCLogger.debug("query: " + query.toString() + " value: "
				+ request.getRequestDefnId());
		List<?> values = getHibernateTemplate().find(query.toString(),
				request.getRequestId());
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiTask task = new UiTask((HstTask) list[column++]);
				task.load((CfgTaskType) list[column++]);
				task.load((CfgTaskCategoryInfo) list[column++]);
				task.load((CfgStepType) list[column++]);
				if (!ASCUtil.isNullOrEmptyObject(task.getCodeId())) {
					task.load(codeDetailsDAO.getCodeDetailsByID(task
							.getCodeId()));
				}
				request.add(task);
			}
		}
	}

	private StringBuffer getQueryTasks(String table) {
		StringBuffer query = new StringBuffer("Select ");
		query.append("	aTask, tType, tCat, stepType ");
		query.append(" from ");
		query.append(table).append(" as aTask , ");
		query.append(CfgTaskType.TABLE_NAME).append(" as tType , ");
		query.append(CfgTaskCategoryInfo.TABLE_NAME).append(" as tCat, ");
		query.append(CfgStepType.TABLE_NAME).append(" as stepType ");
		query.append(" where ");
		query.append(" aTask.").append(ActTask.COL_TASK_TYPE).append(" = ");
		query.append(" tType.").append(CfgTaskType.COL_NAME);
		query.append(" and ");
		query.append(" tType.").append(CfgTaskType.COL_TASK_CATEGORY_ID);
		query.append(" = ");
		query.append(" tCat.").append(CfgTaskCategoryInfo.COL_TASK_CATEGORY_ID);
		query.append(" and ");
		query.append(" aTask.").append(ActTask.COL_STEP_TYPE_ID);
		query.append(" = ");
		query.append(" stepType.").append(CfgStepType.COL_ID);
		query.append(" and ");
		query.append(" aTask.").append(ActTask.COL_REQUEST_ID).append(" = ?");
		query.append(" order by aTask.").append(ActTask.COL_TASK_ID);
		query.append(" desc");
		return query;
	}

	public void loadRequestTypeNDefn(UiRequest request) {
		if (ASCUtil.isNullOrEmptyObject(request.getRequestDefnId())) {
			return;
		}
		StringBuffer query = new StringBuffer("Select ");
		query.append("rType, rDefn ");
		query.append(" from ");
		query.append(CfgRequestType.TABLE_NAME).append(" as rType , ");
		query.append(CfgRequestDefn.TABLE_NAME).append(" as rDefn ");
		query.append(" where ");
		query.append(" rType.").append(CfgRequestType.COL_REQUEST_TYPE_ID);
		query.append(" = ");
		query.append(" rDefn.").append(CfgRequestDefn.COL_TYPE_ID);
		query.append(" and ");
		query.append(" rDefn.").append(CfgRequestDefn.COL_DEFN_ID);
		query.append(" = ?");
		ASCLogger.debug("query: " + query.toString() + " value: "
				+ request.getRequestDefnId());
		List<?> values = getHibernateTemplate().find(query.toString(),
				request.getRequestDefnId());
		if (!ASCUtil.isNullOrEmptyList(values)) {
			int i = 0;
			Object[] list = (Object[]) values.get(0);
			request.load((CfgRequestType) list[i++]);
			request.load((CfgRequestDefn) list[i++]);
		}
	}

	private StringBuffer getRequestListQuery(String table) {
		StringBuffer query = new StringBuffer("Select ");
		query.append("rList, rType ");
		query.append(" from ");
		query.append(CfgRequestType.TABLE_NAME).append(" as rType , ");
		query.append(table).append(" as rList ");
		query.append(" where ");
		query.append(" rType.").append(CfgRequestType.COL_REQUEST_TYPE_ID);
		query.append(" = ");
		query.append(" rList.").append(ActRequestList.COL_REQUEST_TYPE_ID);
		query.append(" and ");
		query.append(" rList.").append(ActRequestList.COL_REQUEST_ID);
		query.append(" = ?");
		return query;
	}

	public UiRequestList getRequestList(String rqListIdStr) {
		Long rqListId = new Long(rqListIdStr);
		UiRequestList rqList = getHstRequestList(rqListId);
		if (ASCUtil.isNullOrEmptyObject(rqList)) {
			rqList = getActRequestList(rqListId);
		}
		return rqList;
	}

	private UiRequestList getHstRequestList(Long rqListId) {
		StringBuffer query = getRequestListQuery(HstRequestList.TABLE_NAME);
		ASCLogger.debug("query: " + query.toString() + " value: " + rqListId);
		List<?> values = getHibernateTemplate()
				.find(query.toString(), rqListId);
		UiRequestList rqList = null;
		if (!ASCUtil.isNullOrEmptyList(values)) {
			int i = 0;
			Object[] list = (Object[]) values.get(0);
			rqList = new UiRequestList((HstRequestList) list[i++]);
			rqList.load((CfgRequestType) list[i++]);

			rqList.setUsers(getUsersForRequestList(
					HstRequestListUser.TABLE_NAME,
					HstRequestListUser.COL_REQUEST_LIST_ID, rqListId));
			rqList.setRequests(getRequestsForRequestList(HstRequest.TABLE_NAME,
					HstRequest.COL_REQUEST_LIST_ID, rqListId));
		}
		return rqList;
	}

	private UiRequestList getActRequestList(Long rqListId) {
		StringBuffer query = getRequestListQuery(ActRequestList.TABLE_NAME);
		ASCLogger.debug("query: " + query.toString() + " value: " + rqListId);
		List<?> values = getHibernateTemplate()
				.find(query.toString(), rqListId);
		UiRequestList rqList = null;
		if (!ASCUtil.isNullOrEmptyList(values)) {
			int i = 0;
			Object[] list = (Object[]) values.get(0);
			rqList = new UiRequestList((ActRequestList) list[i++]);
			rqList.load((CfgRequestType) list[i++]);
			rqList.setUsers(getUsersForRequestList(
					ActRequestListUser.TABLE_NAME,
					ActRequestListUser.COL_REQUEST_LIST_ID, rqListId));
			rqList.setRequests(getRequestsForRequestList(ActRequest.TABLE_NAME,
					ActRequest.COL_REQUEST_LIST_ID, rqListId));
		}
		return rqList;
	}

	private List<UiRqUser> getUsersForRequestList(String table,
			String columnName, Long rqListId) {

		StringBuffer query = new StringBuffer("Select ");
		query.append("rqListUser, code ");
		query.append(" from ");
		query.append(table).append(" as rqListUser , ");
		query.append(CfgCodeDetails.TABLE_NAME).append(" as code ");
		query.append(" where ");
		query.append(" rqListUser.").append(ActRequest.COL_CODE_ID);
		query.append(" = ");
		query.append(" code.").append(CfgCodeDetails.COL_CODE_ID);
		query.append(" and ");
		query.append(" rqListUser.").append(columnName);
		query.append(" = ?");

		ASCLogger.debug("query: " + query.toString() + " value: " + rqListId);
		List<?> values = getHibernateTemplate()
				.find(query.toString(), rqListId);
		List<UiRqUser> users = new ArrayList<UiRqUser>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;

				UiRqUser user = new UiRqUser(
						(AbstractRequestListUser) list[column++]);
				user.load((CfgCodeDetails) list[column++]);
				users.add(user);
			}
		}
		return users;
	}

	private List<UiRequest> getRequestsForRequestList(String table,
			String columnName, Long rqListId) {
		StringBuffer query = getRequestSql(table);
		query.append(" request.").append(columnName);
		query.append(" = ?");

		ASCLogger.debug("query: " + query.toString() + " value: " + rqListId);
		List<?> values = getHibernateTemplate()
				.find(query.toString(), rqListId);
		List<UiRequest> requests = new ArrayList<UiRequest>();
		if (!ASCUtil.isNullOrEmptyList(values)) {
			for (int i = 0; i < values.size(); i++) {
				Object[] list = (Object[]) values.get(i);
				int column = 0;
				UiRequest request = new UiRequest(
						(AbstractRequest) list[column++]);
				request.load((CfgCodeDetails) list[column++]);
				requests.add(request);
			}
		}
		return requests;
	}

	/**
	 * @param codeDetailsDAO
	 *            the codeDetailsDAO to set
	 */
	public void setCodeDetailsDAO(ICodeDetailsDAO codeDetailsDAO) {
		this.codeDetailsDAO = codeDetailsDAO;
	}

	/**
	 * @param stepLogsDAO
	 *            the stepLogsDAO to set
	 */
	public void setStepLogsDAO(IStepLogsDAO stepLogsDAO) {
		this.stepLogsDAO = stepLogsDAO;
	}

	/**
	 * @param fileDetailsDAO
	 *            the fileDetailsDAO to set
	 */
	public void setFileDetailsDAO(IFileTransferDetailsDAO fileDetailsDAO) {
		this.fileDetailsDAO = fileDetailsDAO;
	}
}
